package com.msh.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.base.Strings;
import com.msh.annotations.ProcessType;
import com.msh.common.VMSystem;
import com.msh.contract.server.OrderCheck;
import com.msh.entity.OrderEntity;
import com.msh.service.OrderService;
import com.msh.utils.JsonUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @author :zhangchuanfu
 * @Date :2023/1/16
 * @Time :18:25
 * @description :延迟消息处理
 */
@Component
@ProcessType(value = "orderCheck")
public class OrderCheckHandler implements MsgHandler{
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public void process(String jsonMsg) throws IOException {
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        if (orderCheck == null || Strings.isNullOrEmpty(orderCheck.getOrderNo())) return;

        LambdaQueryWrapper<OrderEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(OrderEntity::getOrderNo, orderCheck.getOrderNo());//代码逻辑修改，因暂时使用不上微信支付，状态直接改为完成
                //.eq(OrderEntity::getStatus, VMSystem.ORDER_STATUS_CREATE);
        OrderEntity order = orderService.getOne(qw);
        if (order == null || order.getStatus()!=VMSystem.ORDER_STATUS_CREATE) return;

        UpdateWrapper<OrderEntity> uw = new UpdateWrapper<>();
        uw.lambda().eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                .set(OrderEntity::getStatus, VMSystem.ORDER_STATUS_INVALID);
        orderService.update(uw);
        //将订单信息同步到es中
        IndexRequest indexRequest = new IndexRequest("order");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",order.getId());
        map.put("status", VMSystem.ORDER_STATUS_INVALID);
        map.put("bill",order.getBill());
        map.put("owner_id",order.getOwnerId());
        map.put("pay_type", order.getPayType());
        map.put("order_no", order.getOrderNo());
        map.put("inner_code", order.getInnerCode());
        map.put("sku_name", order.getSkuName());
        map.put("sku_id", order.getSkuId());
        map.put("pay_status", order.getPayStatus());
        map.put("business_name", order.getBusinessName());
        map.put("business_id", order.getBusinessId());
        map.put("region_id", order.getRegionId());
        map.put("region_name",order.getRegionName());
        map.put("price", order.getPrice());
        map.put("amount",order.getAmount());
        map.put("addr",order.getAddr());
        map.put("open_id", order.getOpenId());
//                mapper.writeValueAsString(order.getCreateTime())
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //map.put("create_time", df.format(order.getCreateTime()));
        map.put("update_time", df.format(order.getUpdateTime()));
        indexRequest.id(order.getId()+"").source(JsonUtil.serialize(map), XContentType.JSON);
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }
}
